<?php
/**
 * Created by PhpStorm.
 * User: godjarvis
 * Date: 2020/08/05
 * Time: 11:56
 */

namespace App\Model\Dao;

use App\Model\Entity\Commission;
use Fresh\Mvc\Model\Dao;
use Fresh\Weird\QueryBuilder;

class CommissionDao extends Dao
{
    public function getUserCommission($dev_id, $startDate = null, $endDate = null)
    {
        return $this->expr()
            ->from(Commission::class)
            ->where('dev_id=?', $dev_id)
            ->whether(function (QueryBuilder $qb) use ($startDate, $endDate) {
                if ($startDate && $endDate) {
                    $qb->rangeDate2Datetime($startDate, $endDate, 'datetime_create');
                }
            })
            ->query()
            ->pluck('sum(commission)');
    }
}
